package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.ChannelType;
import com.rockcent.mall.lib.dto.ChannelTypeDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.ChannelTypeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhangjh on 2016/10/24.
 */
@RestController
public class MerchantChannelTypeController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MerchantChannelTypeController.class);

    @Autowired
    private BaseUserHelper userHelper;

    @Autowired
    private ChannelTypeRepository channelTypeRepository;

     /**
     *
     * @api {get} /merchant/channel/type/list B端 渠道管理 渠道类型 列表
     * @apiGroup merchant_channel
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiParam {Long} parentId   <code>为空表示搜索第一层渠道，传值表示拿到下一级渠道</code>   渠道类型ID
     * @apiSuccessExample {json} Success-Response:
     * {
     *"code": "1",
     *"desc": "成功",
     *"data": [
     *  {
     *      "id": 1,
     *      "dateCreated": "2016-10-24 18:32:54",
     *      "lastUpdated": "2016-10-24 18:33:59",
     *      "version": 1,
     *      "typeName": "线上渠道"
     *   },
     *   {
     *      "id": 2,
     *      "dateCreated": "2016-10-24 18:32:54",
     *      "lastUpdated": "2016-10-24 18:33:59",
     *      "version": 1,
     *      "typeName": "线下渠道"
     *  }
     *  ]
     *   }
     */
    @RequestMapping(value = Routes.MERCHANT_CHANNEL_TYPE_LIST,method = RequestMethod.GET)
    public JsonResult listChannelType(ChannelTypeDto channelTypeDto,HttpServletRequest request) {

        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            List<ChannelType> resultList=null;;
            if(channelTypeDto.getParentId()!=null){
                resultList = channelTypeRepository.findByParentIdAndIsDelete(channelTypeDto.getParentId(),Whether.NO);
            }else {
                resultList = channelTypeRepository.findByParentIdIsNullAndIsDelete(Whether.NO);
            }

            jsonResult.data=resultList;
        };

        return doing.go(request,log);
    }
}
